from PyGameAutoAndroid import *
import time

stability_mode = 0
black_screen_exist = ["#000000", "1|1|#000000", [639, 359, 641, 361]]
battle_coord = [1289, 634]  # 屏幕正中


def wait(t):
    _t = t + 0.2 * stability_mode
    # log("等待：" + str(_t) + "秒")
    time.sleep(_t)


def tap(x, y):
    _t = 0.05 + 0.02 * stability_mode
    # log("点击：" + str(x) + ", " + str(y))
    xp.tap(x, y, _t)
    time.sleep(_t + 0.01)


def moving():
    xp.swipe(1200, 550, 1000, 550)
    wait(0.5)
    xp.swipe(1000, 550, 1200, 550)
    wait(0.5)


def is_battle():
    ret = xp.matchColor("#FFFFFF", 1277, 643)
    if ret:
        return True
    else:
        return False


def check_statement(statement):
    return xp.findColor(statement[0], statement[1], statement[2][0], statement[2][1], statement[2][2], statement[2][3])


def wait_with_checking(statement, max_turns=-1, interval=0.10, inverted=False):
    t = 0
    while True:
        wait(interval)
        t += 1
        if not inverted and check_statement(statement):
            return True
        if inverted and not check_statement(statement):
            return True
        if t == max_turns:
            return False


def tap_preset_coord(coord):
    tap(coord[0], coord[1])


def tap_preset_coord_with_checking(coord, statement, check_per_turn=5, interval=0.10, inverted=False):
    while True:
        tap_preset_coord(coord)
        for i in range(check_per_turn):
            wait(interval)
            if not inverted and check_statement(statement):
                return True
            if inverted and not check_statement(statement):
                return True


def tap_until_black_screen(coord):
    return tap_preset_coord_with_checking(coord, black_screen_exist, check_per_turn=1)


while True:
    moving()
    if is_battle():
        time.sleep(0.2)
        xp.tap(943, 641)
        time.sleep(0.2)
        xp.tap(1058, 647)
        time.sleep(0.2)
        xp.tap(1293, 640)
        # tap_until_black_screen(battle_coord)
